import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('../views/Home.vue'),
    children: [
      {
        path: "/user",
        component: () => import('../views/user'),
        meta: { title: '用户列表' }
      },
      {
        path: '/question',
        component: () => import('../views/question/index'),
        meta: { title: '题库列表' }
      },
      {
        path: "/position/operation",
        component: () => import('../views/question/operation/index.vue'),
        meta: { isNav: false },
        children: [
          {path:'/question/operation/choice',component: () => import('../views/question/operation/choices.vue')},
          {path:'/question/operation/completion',component: () => import('../views/question/operation/completion.vue')},
          {path:'/question/operation/judge',component: () => import('../views/question/operation/judge.vue')},
          {path:'/question/operation/answer',component: () => import('../views/question/operation/answer.vue')}
        ] 
      },
      {
        path: "/achievement",
        component: () => import('../views/achievement'),
        meta: { title: '成绩列表' }
      },
      {
        path: "/catalogue",
        component: () => import('../views/catalogue'),
        meta: { title: '科目列表' }
      },
      {
        path: "/role",
        component: () => import('../views/role'),
        meta: { title: '角色列表' }
      },
      // {
      //   path: "/rule",
      //   component: () => import('../views/rule'),
      //   meta: { title: '权限列表' }
      // },
      {
        path: "/testaper",
        component: () => import('../views/testpaper'),
        meta: { title: '试卷列表' }
      },

      {
        path: "/user/upload",
        component: () => import('../views/replacehead'),
        meta: { isNav: false }
      },
      {
        path: "/user/editpass",
        component: () => import('../views/editpass'),
        meta: { isNav: false }
      },
    ]
  },
  {
    path: "/login", 
    name: "Login",
    component: () => import('../components/login/Login.vue')
  }

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach( async (to, from, next) => {
  let token = window.sessionStorage.getItem("token")
  if (to.path === "/login") {
    if (token) return next("/")
    next()
  } else {
    if (token) return next()
    next("/login")
  }
})

export default router
